﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;


namespace ZX.SYS.Library
{


    /// <summary>
    /// 消息队列表
    /// </summary>
    [Serializable]
    [Table(Name = "V_MessagePool")]
    [ZX.DBModule.Table("V_MessagePool", "MPoolID")]
    public class MessageItem : ZX.DBModule.BaseEntity
    {

        #region Business Methods 业务方法


        
        /// <summary>
        /// 消息ID
        /// </summary>
        private static PropertyInfo<Guid> MPoolIDProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<Guid>("MPoolID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid MPoolID
        {
            get { return GetProperty(MPoolIDProperty); }
            set { SetProperty(MPoolIDProperty, value); }
        }

        /// <summary>
        /// 消息ID
        /// </summary>
        private static PropertyInfo<Guid> MsgIDProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<Guid>("MsgID"));
        [Column]
        public Guid MsgID
        {
            get { return GetProperty(MsgIDProperty); }
            set { SetProperty(MsgIDProperty, value); }
        }

        /// <summary>
        /// 标题
        /// </summary>
        private static PropertyInfo<string> TitleProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<string>("Title"));
        [Column]
        public string Title
        {
            get { return GetProperty(TitleProperty); }
            set { SetProperty(TitleProperty, value); }
        }
        /// <summary>
        /// 消息模板路径
        /// </summary>
        private static PropertyInfo<string> MsgTemplateProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<string>("MsgTemplate"));
        [Column]
        public string MsgTemplate
        {
            get { return GetProperty(MsgTemplateProperty); }
            set { SetProperty(MsgTemplateProperty, value); }
        }
        /// <summary>
        /// 信息内容
        /// </summary>
        private static PropertyInfo<string> MsgContentProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<string>("MsgContent"));
        [Column]
        public string MsgContent
        {
            get { return GetProperty(MsgContentProperty); }
            set { SetProperty(MsgContentProperty, value); }
        }
        /// <summary>
        /// 0:系统;1:Email;2:手机短信;3:QQ
        /// </summary>
        private static PropertyInfo<int> MessageTypeProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<int>("MessageType"));
        [Column]
        public int MessageType
        {
            get { return GetProperty(MessageTypeProperty); }
            set { SetProperty(MessageTypeProperty, value); }
        }
        /// <summary>
        /// 接收人
        /// </summary>
        private static PropertyInfo<Guid> ReceiverIDProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<Guid>("ReceiverID"));
        [Column]
        public Guid ReceiverID
        {
            get { return GetProperty(ReceiverIDProperty); }
            set { SetProperty(ReceiverIDProperty, value); }
        }
        /// <summary>
        /// 接收姓名
        /// </summary>
        private static PropertyInfo<string> UNameProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<string>("Name"));
        [Column]
        public string Name
        {
            get { return GetProperty(UNameProperty); }
            set { SetProperty(UNameProperty, value); }
        }
        /// <summary>
        /// 关联信息来源的编号
        /// </summary>
        private static PropertyInfo<Guid> QueryIDProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<Guid>("QueryID"));
        [Column]
        public Guid QueryID
        {
            get { return GetProperty(QueryIDProperty); }
            set { SetProperty(QueryIDProperty, value); }
        }
       /// <summary>
        /// 视图名
        /// </summary>
        private static PropertyInfo<string> ViewNameProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<string>("ViewName"));
        [Column]
        public string ViewName
        {
            get { return GetProperty(ViewNameProperty); }
            set { SetProperty(ViewNameProperty, value); }
        }
        /// <summary>
        /// 发者送人
        /// </summary>
        private static PropertyInfo<string> AddUserNameProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<string>("AddUserName"));
        [Column]
        public string AddUserName
        {
            get { return GetProperty(AddUserNameProperty); }
            set { SetProperty(AddUserNameProperty, value); }
        }
        /// <summary>
        /// 发送时间
        /// </summary>
        private static PropertyInfo<string> SendTimeProperty = RegisterProperty(typeof(MessageItem), new PropertyInfo<string>("SendTime"));
        [Column]
        public string SendTime
        {
            get { return GetProperty(SendTimeProperty); }
            set { SetProperty(SendTimeProperty, value); }
        }

        #endregion

        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
          
        }
        #endregion

        #region Factory Methods 工厂方法

        public MessageItem() { }

        public static MessageItem New()
        {
            return DataPortal.Create<MessageItem>();
        }
        public static MessageItem Get(Guid MPoolID)
        {
            return DataPortal.Fetch<MessageItem>(new SingleLazyCriteria<MessageItem, Guid>(MPoolID, false));
        }

        #region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid MPoolID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("MPoolID").Equal(MPoolID);

                var data = db.Select<MessageItem>(q)[0];
                LoadProperty(MsgIDProperty, data.MsgID);
                LoadProperty(TitleProperty, data.Title);
                LoadProperty(MsgTemplateProperty, data.MsgTemplate);
                LoadProperty(MsgContentProperty, data.MsgContent);
                LoadProperty(MessageTypeProperty, data.MessageType);
                LoadProperty(ReceiverIDProperty, data.ReceiverID);
                LoadProperty(UNameProperty, data.Name);
                LoadProperty(QueryIDProperty, data.QueryID);
                LoadProperty(ViewNameProperty, data.ViewName);
            }
        }

        #endregion

        #endregion

   

    }
}
